<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="农忙互助订单编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agriculturOrderCode">
              <a-input v-model="model.agriculturOrderCode" placeholder="请输入农忙互助订单编号" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工作量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workload">
              <a-input v-model="model.workload" placeholder="请输入工作量"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="所需人数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="needPeople">
              <a-input v-model="model.needPeople" placeholder="请输入所需人数"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工作简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jobProfile">
              <a-textarea v-model="model.jobProfile" rows="4" placeholder="请输入工作简介" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工作地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jobAddress">
              <a-input v-model="model.jobAddress" placeholder="请输入工作地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工钱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hire">
              <a-input v-model="model.hire" placeholder="请输入工钱"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="接单人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="receiver">
              <a-input v-model="model.receiver" placeholder="请输入接单人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="订单状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jobStatus">
              <j-multi-select-tag type="list_multi" v-model="model.jobStatus" dictCode="order_status" placeholder="请选择订单状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工作类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jobType">
              <j-category-select v-model="model.jobType" pcode="C05" placeholder="请选择工作类型"  />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import {httpAction, getAction, putAction} from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

const ruleBaseURL = '/sys/fillRule/executeRuleByCode/'
  export default {
    name: 'WhtAgriculturAlassistanceInformationForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          jobStatus:"2",
          agriculturOrderCode: null
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           agriculturOrderCode: [
              { required: true, message: '请输入农忙互助订单编号!'},
           ],
           needPeople: [
              { required: true, message: '请输入所需人数!'},
           ],
           jobStatus: [
              { required: true, message: '请输入订单状态!'},
           ],
        },
        url: {
          add: "/whtNongMang/whtAgriculturAlassistanceInformation/add",
          edit: "/whtNongMang/whtAgriculturAlassistanceInformation/edit",
          queryById: "/whtNongMang/whtAgriculturAlassistanceInformation/queryById",
          rule: {
            orderNum: ruleBaseURL + 'nongmang_order_num'
          },
        },
        code:null
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      console.log( this.modelDefault)

    },
    methods: {
      add () {
        this.edit(this.modelDefault);
        this.getOrderNum()

      },
      edit (record) {
        // console.log(record)
        this.code=record.agriculturOrderCode;
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      getOrderNum() {
        if (this.code==null){
          putAction(this.url.rule.orderNum, this.model).then(res => {
            // 执行成功，获取返回的值，并赋到页面上
            if (res.success) {
              console.log(res)
              this.model.agriculturOrderCode = res.result
            }
          });
        }

      },
      handleCategoryChange(value,backObj){
         this.model = Object.assign(this.model, backObj);
      }
    }
  }
</script>